<template>
  <div class="meeting-approval">
    <van-nav-bar
      title="会议审批"
      left-arrow
      :safe-area-inset-top="true"
      @click-left="onClickLeft"
    >
    </van-nav-bar>

    <div class="approval-body" v-if="approvalMeetingList.length === 0">
      <van-pull-refresh
        style="height: 100%"
        v-model="refreshLoading"
        success-text="刷新成功！"
        @refresh="onRefresh"
        ><van-empty image-size="12rem" description="暂无会议申请" />
      </van-pull-refresh>
    </div>
    <div class="approval-body" v-else>
      <van-pull-refresh
        class="refresh"
        style="height: 100%"
        v-model="refreshLoading"
        success-text="刷新成功！"
        @refresh="onRefresh"
      >
        <div class="item" v-for="item in approvalMeetingList" :key="item.id">
          <div class="title">发起人：{{item.userName}}</div>
          <div class="content">
            <div class="content-item"> 
              <span class="label">主题</span>
              <span class="value theme">{{ item.name }}</span>
            </div>
            <div class="content-item">
              <span class="label">时间</span>
              <span class="value">{{judgeIsSameDay(item.startTime, item.endTime, 0)}} {{ fromatDateTime(item.startTime) }} {{judgeIsSameDay(item.startTime, item.endTime, 1)}} {{ fromatDateTime(item.endTime) }}</span>
            </div>
              <div class="content-item" v-if="item.type === 0">
                <span class="label">会议类型</span>
                <span class="value">线下会议</span>
              </div>
            <div class="content-item" v-else>
              <span class="label">房间号</span>
              <span class="value">{{item.roomNumber}}</span>
            </div>
          </div>
          <div class="operation">
            <span class="detail" @click="goMeetingDetail(item)">详情</span>
            <span class="through" v-if="item.meetingStatus === 1"
              >已通过</span
            >
            <span class="refused" v-if="item.meetingStatus === 2"
              >已拒绝</span
            >
            <span class="to-refused" @click="goApproval(item, 2)" v-if="item.meetingStatus === 0"
              >拒绝</span
            >
            <span class="to-through" @click="goApproval(item, 1)" v-if="item.meetingStatus === 0"
              >通过</span
            >
          </div>
        </div>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
import { onMounted, ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import {
  getCmsMeeting,
  updateCmsMeetingById,
} from '@/api/home/meetingApproval.js'
import { fromatDateTime, judgeIsSameDay } from "@/utils/formatDate.js"
import { Notify } from 'vant'
export default {
  name: 'MeetingApproval',
  setup() {
    const router = useRouter()
    const store = useStore()

    const userNumber = computed(() => store.getters.getUserNumber)

    onMounted(() => {
      getApprovalMeeting()
    })

    let refreshLoading = ref(false)

    // 刷新
    function onRefresh() {
      getApprovalMeeting()
    }

    let approvalMeetingList = ref([])

    // 获取待审批会议
    async function getApprovalMeeting() {
      const res = await getCmsMeeting({ meetingStatus: 0 })
      console.log(res)
      if (res.data.code === 400) {
        Notify({ type: 'warning', duration: 1000, message: res.data.message })
        return
      }

      approvalMeetingList.value.length = 0  

      // 获取除去本人的审批会议
      res.data.data.forEach(t => {
        if (t.userNumber !== userNumber.value) {
          approvalMeetingList.value.push(t)
        }
      })
      refreshLoading.value = false
    }

    // 审批会议
    async function goApproval(item, value) {
      const res = await updateCmsMeetingById({
        meetingStatus: value,
        id: item.id,
      })
      console.log(res)
      if (res.data.code === 400) {
        Notify({ type: 'danger', duration: 1000, message: res.data.message })
        return
      }
      Notify({
        type: 'success',
        duration: 1000,
        message: `已${value === 1 ? '通过' : '拒绝'}${item.userName}会议申请`,
      })
      getApprovalMeeting()
    }

    // 前往会议详情
    function goMeetingDetail(item) {
      router.push({
        path: '/meetingdetail',
        query: {
          id: item.id,
          type: 1,
        },
      })
    }

    const onClickLeft = () => history.back()

    return {
      approvalMeetingList,
      refreshLoading,
      goApproval,
      goMeetingDetail,
      onClickLeft,
      onRefresh,
      fromatDateTime,
      judgeIsSameDay
    }
  },
}
</script>

<style lang="scss" scoped>
.meeting-approval {
  width: 100%;
  height: 100vh;
  background-color: var(--approval-background-color);

  .approval-body {
    width: 100%;
    height: calc(100vh - var(--van-nav-bar-height));

    .refresh {
      width: 100%;
      height: 100%;
      overflow: auto;
      padding: 1rem;
      box-sizing: border-box;

      .item {
        width: 100%;
        height: 20rem;
        margin-bottom: 1rem;
        border-radius: 1rem;
        // background-color: #fff;
        background-color: var(--approval-item-background-color);
        box-sizing: border-box;
        padding: 0 1rem;
        display: flex;
        flex-direction: column;

        .title {
          height: 4rem;
          line-height: 4rem;
          width: 100%;
          border-bottom: 1px solid #EEE;
          font-size: 1.4rem;
          font-weight: bold;
        }

        .content {
          flex: 1;
          display: flex;
          flex-direction: column;
          border-bottom: 0.1rem solid #EEE;
          box-sizing: border-box;
          padding: 1.5rem 0;

          &-item {
            margin-top: 1rem;
            font-size: 1.4rem;
            width: 100%;
            display: flex;
        
            
            .label {
              color: var(--second-text-color);
              width: 8rem;
              display: inline-block;
            }

            .value {
              flex: 1;
              display: inline-block;
              overflow: hidden;
              word-wrap: break-word;
              // line-height: 1.5rem;
            }
            .theme {
              font-weight: bold;
              font-size: 1.5rem;
            }
          }

        
        }
        .operation {
          height: 5rem;
          width: 100%;
          display: -webkit-box;
          display: -webkit-flex;
          display: -ms-flexbox;
          display: flex;
          -webkit-box-align: center;
          -ms-flex-align: center;
          align-items: center;

          -webkit-box-pack: right;
          -ms-flex-pack: right;
          justify-content: right;
          

          span {
            margin-right: 1rem;
            width: 6rem;
            height: 2.8rem;
            line-height: 2.8rem;
            text-align: center;
            border-radius: 0.5rem;
          }

          .to-through {
            background-color: var(--success-btn-bg);
            color: var(--success-btn-color);
          }
          .to-refused {
            background-color: var(--refused-btn-bg);
            color: var(--refused-btn-color);
          }
          .through {
            background-color: #67C23A;
            color: #FFF;
          }
          .refused {
            background-color: #F56C6C;
            color: #FFF;
          }
          .detail {
            background-color: #f6f6fb;
            color: #000;
          }
        }
      }
    }

    
  }
}
</style>